﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using CultureInfoEntry=System.Tuple<string, string, string>;

namespace TrafficWatcher.Views.Helpers
{
    public static class DurationLocalizer
    {
        private static readonly IList<CultureInfoEntry> CultureInfoEntries = new List<CultureInfoEntry>
            {
                new CultureInfoEntry("en-US",  @"m\m\i\n", @"h\hm\m\i\n"),
                new CultureInfoEntry("ru-RU",  @"m\м\и\н", @"h\ч\ m\м\и\н")
            };

        public static string ToString(TimeSpan duration)
        {
            return ToString(duration, CultureInfo.CurrentUICulture);
        }

        public static string ToString(TimeSpan duration, CultureInfo culture)
        {
            return (from entry in CultureInfoEntries
                    where culture.Equals(new CultureInfo(entry.Item1))
                    select duration.ToString((int)duration.TotalHours == 0 ? entry.Item2 : entry.Item3))
                        .FirstOrDefault() ?? ToString(duration, new CultureInfo("en-US"));
        }
    }
}
